﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyLibrary.AR;
using EasyLibrary.AR.Classes;

namespace EasyFrota_Mobile
{
    public partial class ListaViaturas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList ViaturaTipoList = new ArrayList();
                var vt = new ViaturaTipo();
                ViaturaTipoList = vt.LoadAll();
                DropDownAdd(ViaturaTipoList);

                IList empList = new ArrayList();
                var emp = new Empresa();
                empList = emp.LoadAll();
                DropDownAdd2(empList);

                LoadTabela();
            }
        }

        private void DropDownAdd(IList viaturaTipoList)
        {
            dropdownTipo.Items.Add(new ListItem("Todos", "0"));
            foreach (ViaturaTipo viaTipo in viaturaTipoList)
            {
                dropdownTipo.Items.Add(new ListItem(viaTipo.TipoViatura, viaTipo.Id.ToString()));
            }
        }

        private void DropDownAdd2(IList empList)
        {
            dropdownEmpresa.Items.Add(new ListItem("Todas", "0"));
            foreach (Empresa emp in empList)
            {
                dropdownEmpresa.Items.Add(new ListItem(emp.Nome, emp.IDEmpresa.ToString()));
            }
        }

        public void LoadTabela()
        {
            StringBuilder sb = new StringBuilder();
            DataSet viaturas = new DataSet();
            hf_tipo.Value = dropdownTipo.SelectedValue;
            hf_kit.Value = dropdownKit.SelectedValue;
            hf_Empresa.Value = dropdownEmpresa.SelectedValue;
            ViaturaCollection vc = new ViaturaCollection();

            viaturas = vc.LoadAll(hf_kit.Value, hf_Empresa.Value, hf_tipo.Value);
            listVia.Text = "";
            int i = 1;

            if (viaturas.Tables[0].Rows.Count == 0)
            {
                
              Response.Write(@"<script> alert ('Coiso'); <");
            }
            else
            {
                foreach (DataRow dr in viaturas.Tables[0].Rows)
                {
                    listVia.Text += "<tr>";
                    listVia.Text += "<th>" + i + "</th>";
                    listVia.Text += "<td>" + dr["matricula"].ToString() + "</td>";
                    listVia.Text += "<td>" + dr["Modelo"].ToString() + "</td>";
                    listVia.Text += "<td>" + dr["Marca"].ToString() + "</td>";
                    listVia.Text += "<td>" + dr["Condutor"].ToString() + "</td>";
                    listVia.Text += "<td>" + dr["Estado"].ToString() + "</td>";

                    listVia.Text += "</tr>";
                    i++;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadTabela();
        }
    }
}